<template>
  <div class="login-container">
    <div class="login-panel">
      <div class="company-info">
        <div class="logo-container">
          <img src="@/assets/logo/logo.png" alt="Logo" class="logo" v-if="false">
          <h2 class="system-name">{{title}}</h2>
        </div>
        <div class="slogan">企业级通用管理系统平台</div>
      </div>
      <div class="login-form-container">
        <div class="welcome-text">
          <h3>欢迎登录</h3>
          <p>请使用您的账号和密码登录系统</p>
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
              prefix-icon="el-icon-user"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              prefix-icon="el-icon-lock"
              @keyup.enter.native="handleLogin"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <div class="captcha-container">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                prefix-icon="el-icon-key"
                @keyup.enter.native="handleLogin"
              >
              </el-input>
              <div class="captcha-img" @click="getCode">
                <img :src="codeUrl" class="captcha-image"/>
              </div>
            </div>
          </el-form-item>
          <div class="login-options">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <a href="javascript:void(0)" class="forgot-password">忘记密码?</a>
          </div>
          <el-form-item style="margin-bottom: 10px;">
            <el-button
              :loading="loading"
              type="primary"
              class="login-button"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
          <div class="extra-options" v-if="register">
            <span>还没有账号?</span>
            <router-link class="register-link" :to="'/register'">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
    <div class="copyright">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: '',
      loginForm: {
        username: 'admin',
        password: 'admin123',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
}

.login-panel {
  display: flex;
  width: 900px;
  height: 500px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.company-info {
  width: 40%;
  background-color: #1890ff;
  background-image: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  padding: 40px;
  color: white;
  display: flex;
  flex-direction: column;
}

.logo-container {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.logo {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.system-name {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.slogan {
  font-size: 16px;
  opacity: 0.9;
  flex-grow: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 300;
  letter-spacing: 1px;
}

.login-form-container {
  width: 60%;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.welcome-text {
  margin-bottom: 30px;
  h3 {
    font-size: 24px;
    color: #303133;
    margin: 0 0 8px 0;
    font-weight: 500;
  }
  p {
    color: #909399;
    font-size: 14px;
    margin: 0;
  }
}

.login-form {
  width: 100%;
  .el-form-item {
    margin-bottom: 25px;
  }
  .el-input {
    height: 40px;
    ::v-deep .el-input__inner {
      height: 40px;
      line-height: 40px;
      border-radius: 4px;
    }
  }
}

.captcha-container {
  display: flex;
  .el-input {
    flex: 1;
  }
  .captcha-img {
    width: 120px;
    height: 40px;
    margin-left: 10px;
    cursor: pointer;
    background: #f5f7fa;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    overflow: hidden;
  }
  .captcha-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  .forgot-password {
    color: #1890ff;
    text-decoration: none;
    font-size: 14px;
    &:hover {
      color: #40a9ff;
    }
  }
}

.login-button {
  width: 100%;
  height: 40px;
  border-radius: 4px;
  font-size: 16px;
}

.extra-options {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #606266;
  .register-link {
    color: #1890ff;
    text-decoration: none;
    margin-left: 5px;
    &:hover {
      color: #40a9ff;
    }
  }
}

.copyright {
  position: absolute;
  bottom: 20px;
  color: #606266;
  font-size: 12px;
}

// 响应式调整
@media screen and (max-width: 992px) {
  .login-panel {
    width: 80%;
    height: auto;
    flex-direction: column;
  }
  .company-info, .login-form-container {
    width: 100%;
  }
  .company-info {
    padding: 30px;
    height: 150px;
  }
}

@media screen and (max-width: 768px) {
  .login-panel {
    width: 90%;
  }
}
</style>
